<template>
  <a-spin :spinning="loading" :delay="300" tip="downloading" size="large"></a-spin>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, UnwrapRef } from 'vue'
import { getRoot } from '/@/root'
import { login, LoginBody } from '/@/api/manage'
import { ELocalStorageKey, ERouterName, EUserType } from '/@/types'
import { message } from 'ant-design-vue'
const loading = ref(false)
const root = getRoot()

const formState: UnwrapRef<LoginBody> = reactive({
  username: 'adminPC',
  password: 'adminPC',
  flag: EUserType.Web,
})
const onSubmit = async (e: any) => {
  loading.value = true
  const result = await login(formState)
  loading.value = false
  if (result.code === 0) {
    localStorage.setItem(ELocalStorageKey.Token, result.data.access_token)
    localStorage.setItem(ELocalStorageKey.WorkspaceId, result.data.workspace_id)
    localStorage.setItem(ELocalStorageKey.Username, result.data.username)
    localStorage.setItem(ELocalStorageKey.UserId, result.data.user_id)
    localStorage.setItem(ELocalStorageKey.Flag, EUserType.Web.toString())
    root.$router.push(ERouterName.MEMBERS)
  } else {
    message.error(result.message)
  }
}
onMounted(() => {
  onSubmit({})
})
</script>

<style scoped>

</style>
